package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysAppBookchaptersMapper;
import com.ruoyi.system.domain.SysAppBookchapters;
import com.ruoyi.system.service.ISysAppBookchaptersService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.BusinessException;

/**
 * 书籍章节Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-09-07
 */
@Service
public class SysAppBookchaptersServiceImpl implements ISysAppBookchaptersService 
{
    @Autowired
    private SysAppBookchaptersMapper sysAppBookchaptersMapper;

    /**
     * 查询书籍章节
     * 
     * @param deptId 书籍章节ID
     * @return 书籍章节
     */
    @Override
    public SysAppBookchapters selectSysAppBookchaptersById(Long deptId)
    {
        return sysAppBookchaptersMapper.selectSysAppBookchaptersById(deptId);
    }

    /**
     * 查询书籍章节列表
     * 
     * @param sysAppBookchapters 书籍章节
     * @return 书籍章节
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<SysAppBookchapters> selectSysAppBookchaptersList(SysAppBookchapters sysAppBookchapters)
    {
        return sysAppBookchaptersMapper.selectSysAppBookchaptersList(sysAppBookchapters);
    }

    /**
     * 新增书籍章节
     * 
     * @param sysAppBookchapters 书籍章节
     * @return 结果
     */
    @Override
    public int insertSysAppBookchapters(SysAppBookchapters sysAppBookchapters)
    {
    	SysAppBookchapters info = sysAppBookchaptersMapper.selectSysAppBookchaptersById(sysAppBookchapters.getParentId());
        // 如果父节点不为"正常"状态,则不允许新增子节点
    	if(null != info) {
    		if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())){
                throw new BusinessException("章节停用，不允许新增");
            }
    		sysAppBookchapters.setAncestors(info.getAncestors() + "," + sysAppBookchapters.getParentId());
    	}else {
    		sysAppBookchapters.setAncestors(sysAppBookchapters.getParentId()+"");
    	}
        sysAppBookchapters.setCreateTime(DateUtils.getNowDate());
        return sysAppBookchaptersMapper.insertSysAppBookchapters(sysAppBookchapters);
    }

    /**
     * 修改书籍章节
     * 
     * @param sysAppBookchapters 书籍章节
     * @return 结果
     */
    @Override
    public int updateSysAppBookchapters(SysAppBookchapters sysAppBookchapters)
    {
        sysAppBookchapters.setUpdateTime(DateUtils.getNowDate());
        return sysAppBookchaptersMapper.updateSysAppBookchapters(sysAppBookchapters);
    }

    /**
     * 删除书籍章节对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysAppBookchaptersByIds(String ids)
    {
        return sysAppBookchaptersMapper.deleteSysAppBookchaptersByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除书籍章节信息
     * 
     * @param deptId 书籍章节ID
     * @return 结果
     */
    @Override
    public int deleteSysAppBookchaptersById(Long deptId)
    {
        return sysAppBookchaptersMapper.deleteSysAppBookchaptersById(deptId);
    }

	@Override
	public int selectSysAppBookchaptersCount(SysAppBookchapters sysAppBookchapters) {
		int i = sysAppBookchaptersMapper.selectSysAppBookchaptersCount(sysAppBookchapters);
		return i;
	}

	@Override
	public SysAppBookchapters selectContentById(Long deptId) {
		SysAppBookchapters info = sysAppBookchaptersMapper.selectContentById(deptId);
		return info;
	}
}
